package com.ym.utils;


import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class PDDUtils {

    private static String url = "http://gw-api.pinduoduo.com/api/router?";
    private static String client_id = "8dda145144d74c01bff14bf64fb25fc1";
    private static String client_secret = "d51a63867f7c5618766e0a9a5bcd22c1cfaf978f";

    private static String client2_id = "12dedb0339d245ca9bdcb373467e2660";
    private static String client2_secret = "005af29fc4a7ed248e69117af9e2f6581f6999c8";

    private static String clientShop_id = "4b7c698de0f9446eaf99b3c5b34c6a83";
    private static String clientShop_secret = "19edb0cbb320b51f95b2b4126f4d7697f66e5b5c";

    public static void main(String[] args) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("type","pdd.ddk.goods.search");
        paramMap.put("sort_type","0");
        paramMap.put("with_coupon","true");
        String resulet = HttpUtils.client(postUrl(paramMap));
//        System.out.println(resulet);
        JSONObject job = new JSONObject(resulet);
        Object data = job.get("goods_search_response");
        job = new JSONObject(data.toString());
        JSONArray janswers = job.getJSONArray("goods_list");
        Iterator<Object> ianswers  = janswers.iterator();
        while (ianswers.hasNext()) {
            StringBuffer buf = new StringBuffer();
            String str = ianswers.next().toString();
            JSONObject obj =  new JSONObject(str);
            Iterator<String> nameList = obj.keys();
            while (nameList.hasNext()) {
                String key = nameList.next().toString();
                String value = obj.get(key).toString();
                System.out.println(key+"="+value+"\r");
            }
            System.out.println("<------------------------------------------------------------------------------------>");

        }
//        System.out.println(janswers);
    }


    /**
     * 拼装后的URL地址
     * @param paramMap
     * @return
     */
    public static String postUrl(Map<String,Object> paramMap){
        StringBuffer postUrl = new StringBuffer(url);
        Map<String,Object> treeMap = new TreeMap<>();
        for (Map.Entry<String, Object> m : paramMap.entrySet()) {
            treeMap.put(m.getKey(),m.getValue());
        }
        treeMap.put("client_id",client_id);
        treeMap.put("data_type","JSON");
        treeMap.put("timestamp",System.currentTimeMillis());
        String pStr = maptoStr(treeMap);
        System.out.println("pStr="+pStr);
        String sign = PublicUtils.MD5(pStr.toString()).toUpperCase();
        treeMap.put("sign",sign);
        for (Map.Entry<String, Object> m : treeMap.entrySet()) {
            postUrl.append(m.getKey());
            postUrl.append("=");
            postUrl.append(m.getValue());
            postUrl.append("&");
        }
//        System.out.println("postUrl:"+postUrl.toString());
        return postUrl.toString();
    }

    //map转换成字符串
    private static String maptoStr(Map<String, Object> treeMap) {
        StringBuffer pStr = new StringBuffer();
        pStr.append(client_secret);
        for (Map.Entry<String, Object> m : treeMap.entrySet()) {
            pStr.append(m.getKey());
            pStr.append(m.getValue());
        }
        pStr.append(client_secret);
        return pStr.toString();
    }


    /**
     * 拼装后的URL地址
     * @param paramMap
     * @return
     */
    public static String postNewUrl(Map<String,Object> paramMap){
        StringBuffer postUrl = new StringBuffer(url);
        Map<String,Object> treeMap = new TreeMap<>();
        for (Map.Entry<String, Object> m : paramMap.entrySet()) {
            treeMap.put(m.getKey(),m.getValue());
        }
        treeMap.put("client_id",client2_id);
        treeMap.put("data_type","JSON");
        treeMap.put("timestamp",System.currentTimeMillis());
        String pStr = maptoNewStr(treeMap);
        //System.out.println("pStr="+pStr);
        String sign = PublicUtils.MD5(pStr.toString()).toUpperCase();
        treeMap.put("sign",sign);
        for (Map.Entry<String, Object> m : treeMap.entrySet()) {
            postUrl.append(m.getKey());
            postUrl.append("=");
            if("url".equals(m.getKey())) {
                postUrl.append(m.getValue().toString().replaceAll("&", "%26"));
            }else{
                postUrl.append(m.getValue());
            }
            postUrl.append("&");
        }
        return postUrl.toString();
    }

    //map转换成字符串
    private static String maptoNewStr(Map<String, Object> treeMap) {
        StringBuffer pStr = new StringBuffer();
        pStr.append(client2_secret);
        for (Map.Entry<String, Object> m : treeMap.entrySet()) {
            pStr.append(m.getKey());
            pStr.append(m.getValue());
        }
        pStr.append(client2_secret);
        return pStr.toString();
    }

    /**
     * 拼装后的URL地址
     * @param paramMap
     * @return
     */
    public static String postShop(Map<String,Object> paramMap){
        StringBuffer postUrl = new StringBuffer(url);
        Map<String,Object> treeMap = new TreeMap<>();
        for (Map.Entry<String, Object> m : paramMap.entrySet()) {
            treeMap.put(m.getKey(),m.getValue());
        }
        treeMap.put("client_id",clientShop_id);
        treeMap.put("data_type","JSON");
        treeMap.put("timestamp",System.currentTimeMillis());
        String pStr = maptoShopStr(treeMap);
        //System.out.println("pStr="+pStr);
        String sign = PublicUtils.MD5(pStr.toString()).toUpperCase();
        treeMap.put("sign",sign);
        for (Map.Entry<String, Object> m : treeMap.entrySet()) {
            postUrl.append(m.getKey());
            postUrl.append("=");
            if("url".equals(m.getKey())) {
                postUrl.append(m.getValue().toString().replaceAll("&", "%26"));
            }else{
                postUrl.append(m.getValue());
            }
            postUrl.append("&");
        }
        return postUrl.toString();
    }

    //map转换成字符串
    private static String maptoShopStr(Map<String, Object> treeMap) {
        StringBuffer pStr = new StringBuffer();
        pStr.append(clientShop_secret);
        for (Map.Entry<String, Object> m : treeMap.entrySet()) {
            pStr.append(m.getKey());
            pStr.append(m.getValue());
        }
        pStr.append(clientShop_secret);
        return pStr.toString();
    }
}
